package com.readbook.action;


import com.alibaba.fastjson.JSONArray;
import com.readbook.service.ScyyService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/scyy")
public class scyy {
    @Resource
    private ScyyService scyyService;

    /**
     * @author 牟明艳
     * @comments  诗词语音搜索
     * @time 2019年11月14日23:16:29
     */
    @RequestMapping("Search_scyy.html")
    @ResponseBody
    public Map<String, Object> Search_scyy(HttpServletRequest request, HttpServletResponse response) {
        //诗词语音名称
        String pname = "";
        if (request.getParameter("pname") != null) {
            pname = request.getParameter("pname").toString().trim();
        }
        //作者
        String author = "";
        if (request.getParameter("author") != null) {
            author = request.getParameter("author").toString().trim();
        }
        //年级
        String nj = "";
        if (request.getParameter("nj") != null) {
            nj = request.getParameter("nj").toString().trim();
        }
        //班级级
        String bj = "";
        if (request.getParameter("bj") != null) {
            bj = request.getParameter("bj").toString().trim();
        }



        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("pname", pname);
        gmap.put("author", author);
        gmap.put("nj", nj);
        gmap.put("bj", bj);
        HttpSession session = ((HttpServletRequest) request).getSession();
        if (session.getAttribute("usid") != null) {
            gmap.put("usid", session.getAttribute("usid").toString());
        }

        List<Map<String, Object>> list1 = scyyService.Search_scyy(gmap);
        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("data", list1);
        return rmap;
    }



    /**
     * @author 牟明艳
     * @comments  诗词语音删除
     * @time 2019年11月14日23:16:40
     */
    @RequestMapping("del_scyyByid.html")
    @ResponseBody
    public Map<String, Object> del_scyyByid(HttpServletRequest request, HttpServletResponse response) {
        String readid = request.getParameter("readid").trim();
        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("readid", readid);
        Map<String, Object> getm = scyyService.del_scyyByid(gmap);
        return getm;
    }


    /**
     * @author 牟明艳
     * @comments  诗词语音插入
     * @time 2019年11月15日18:25:28
     */
    @RequestMapping("insert_scyy.html")
    @ResponseBody
    public Map<String, Object> insert_scyy(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> gmap = new HashMap<String, Object>();
        //诗词语音名称
        String pname = "";
        if (request.getParameter("pname") != null) {
            pname = request.getParameter("pname").toString().trim();
        }
        //作者
        String author = "";
        if (request.getParameter("author") != null) {
            author = request.getParameter("author").toString().trim();
        }
        //年级
        String nj = "";
        if (request.getParameter("nj") != null) {
            nj = request.getParameter("nj").toString().trim();
        }
        //班级
        String bj = "";
        if (request.getParameter("bj") != null) {
            bj = request.getParameter("bj").toString().trim();
        }
        //诗词内容
        String pcontent = "";
        if (request.getParameter("pcontent") != null) {
            pcontent = request.getParameter("pcontent").toString().trim();
        }
        //诗词封面
        String fm = "";
        if (request.getParameter("fm") != null) {
            fm = request.getParameter("fm").toString().trim();
        }
        //是否发布
        String bstate = "";
        if (request.getParameter("bstate") != null) {
            bstate = request.getParameter("bstate").toString().trim();
        }

        //发布人
        HttpSession session = ((HttpServletRequest) request).getSession();
        if (session.getAttribute("usid") != null) {
            gmap.put("publisher", session.getAttribute("usid").toString());
        }

        String readid = UUID.randomUUID().toString().replaceAll("-", "");
        gmap.put("readid", readid);
        gmap.put("pname", pname);
        gmap.put("author", author);
        gmap.put("nj", nj);
        gmap.put("bj", bj);
        gmap.put("pcontent", pcontent);
        gmap.put("fm", fm);
        gmap.put("bstate", bstate);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        gmap.put("pubilshtime", df.format(new Date()));
        int i = scyyService.savePoteryRead(gmap);

        Map<String, Object> remap = new HashMap<String, Object>();
        if (i > 0) {
            //诗词语音
            String listyy = "";
            if (request.getParameter("listyy") != null) {
                listyy = request.getParameter("listyy").toString().trim();
            }
            //音频文件
            if (!listyy.equals("")) {
                List<Map<String, Object>> listmusic = (List<Map<String, Object>>) JSONArray.parse(listyy);
                for (int k = 0; k < listmusic.size(); k++) {
                    Map<String, Object> ypmap = new HashMap<String, Object>();
                    ypmap.put("readid", readid);
                    ypmap.put("yp", listmusic.get(k).get("YP"));
                    ypmap.put("mc", listmusic.get(k).get("MC"));
                    ypmap.put("xh", k + "");
                    scyyService.savePoetryreadYp(ypmap);
                }
            }

            //诗词内容
            String listsc = "";
            if (request.getParameter("listsc") != null) {
                listsc = request.getParameter("listsc").toString().trim();
            }
            if (!listsc.equals("")) {
                //诗词内容文件
                List<Map<String, Object>> lissc = (List<Map<String, Object>>) JSONArray.parse(listsc);
                for (int m = 0; m < lissc.size(); m++) {
                    String code = "poetryread";
                    String codename = "pcontent";
                    Map<String, Object> imap = new HashMap<String, Object>();
                    imap.put("picurl", lissc.get(m).get("PICURL"));
                    imap.put("code", code);
                    imap.put("codename", codename);
                    imap.put("pid", readid);
                    imap.put("xh", m + "");
                    scyyService.savePicture(imap);
                }
            }
            remap.put("msg", "保存成功!");
            remap.put("result", true);
        } else {
            remap.put("msg", "保存失败!");
            remap.put("result", false);
        }
        return remap;
    }





    /**
     * @author 牟明艳
     * @comments  诗词语音修改
     * @time 2019年11月15日18:25:28
     */
    @RequestMapping("edit_scyy.html")
    @ResponseBody
    public Map<String, Object> edit_scyy(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> gmap = new HashMap<String, Object>();
        String readid = "";
        if (request.getParameter("readid") != null) {
            readid = request.getParameter("readid").toString().trim();
        }
        //诗词语音名称
        String pname = "";
        if (request.getParameter("pname") != null) {
            pname = request.getParameter("pname").toString().trim();
        }
        //作者
        String author = "";
        if (request.getParameter("author") != null) {
            author = request.getParameter("author").toString().trim();
        }
        //年级
        String nj = "";
        if (request.getParameter("nj") != null) {
            nj = request.getParameter("nj").toString().trim();
        }
        //班级
        String bj = "";
        if (request.getParameter("bj") != null) {
            bj = request.getParameter("bj").toString().trim();
        }
        //诗词内容
        String pcontent = "";
        if (request.getParameter("pcontent") != null) {
            pcontent = request.getParameter("pcontent").toString().trim();
        }
        //诗词封面
        String fm = "";
        if (request.getParameter("fm") != null) {
            fm = request.getParameter("fm").toString().trim();
        }
        //是否发布
        String bstate = "";
        if (request.getParameter("bstate") != null) {
            bstate = request.getParameter("bstate").toString().trim();
        }

        gmap.put("readid", readid);
        gmap.put("pname", pname);
        gmap.put("author", author);
        gmap.put("nj", nj);
        gmap.put("bj", bj);
        gmap.put("pcontent", pcontent);
        gmap.put("fm", fm);
        gmap.put("bstate", bstate);
        int i = scyyService.editPoteryRead(gmap);

        Map<String, Object> remap = new HashMap<String, Object>();
        if (i > 0) {
            //删除音频文件
            scyyService.delPoetryreadYp(gmap);
            //诗词语音
            String listyy = "";
            if (request.getParameter("listyy") != null) {
                listyy = request.getParameter("listyy").toString().trim();
            }
            //音频文件
            if (!listyy.equals("")) {
                List<Map<String, Object>> listmusic = (List<Map<String, Object>>) JSONArray.parse(listyy);
                for (int k = 0; k < listmusic.size(); k++) {
                    Map<String, Object> ypmap = new HashMap<String, Object>();
                    ypmap.put("readid", readid);
                    ypmap.put("yp", listmusic.get(k).get("YP"));
                    ypmap.put("mc", listmusic.get(k).get("MC"));
                    ypmap.put("xh", k + "");
                    scyyService.savePoetryreadYp(ypmap);
                }
            }

            //诗词内容
            //删除诗词内容文件
            scyyService.delScPicture(gmap);
            String listsc = "";
            if (request.getParameter("listsc") != null) {
                listsc = request.getParameter("listsc").toString().trim();
            }
            if (!listsc.equals("")) {
                //诗词内容文件
                List<Map<String, Object>> lissc = (List<Map<String, Object>>) JSONArray.parse(listsc);
                for (int m = 0; m < lissc.size(); m++) {
                    String code = "poetryread";
                    String codename = "pcontent";
                    Map<String, Object> imap = new HashMap<String, Object>();
                    imap.put("picurl", lissc.get(m).get("PICURL"));
                    imap.put("code", code);
                    imap.put("codename", codename);
                    imap.put("pid", readid);
                    imap.put("xh", m + "");
                    scyyService.savePicture(imap);
                }
            }
            remap.put("msg", "保存成功!");
            remap.put("result", true);
        } else {
            remap.put("msg", "保存失败!");
            remap.put("result", false);
        }
        return remap;
    }


    /**
     * @author 牟明艳
     * @comments  诗词语音提交
     * @time 2019年11月16日13:33:41
     */
    @RequestMapping("update_scyy_bstate.html")
    @ResponseBody
    public Map<String, Object> update_scyy_bstate(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> gmap = new HashMap<String, Object>();
        String readid = "";
        if (request.getParameter("readid") != null) {
            readid = request.getParameter("readid").toString().trim();
        }
        //诗词状态
        String bstate = "";
        if (request.getParameter("bstate") != null) {
            bstate = request.getParameter("bstate").toString().trim();
        }
        gmap.put("readid", readid);
        gmap.put("bstate", bstate);
        int i = scyyService.editPoteryRead(gmap);
        Map<String, Object> remap = new HashMap<String, Object>();
        if (i > 0) {
            remap.put("msg", "提交成功!");
            remap.put("result", true);
        } else {
            remap.put("msg", "提交失败!");
            remap.put("result", false);
        }
        return remap;
    }
}
